package org.example.managementsystem.controller;

import org.example.managementsystem.model.ProviderApplication;
import org.example.managementsystem.model.ProviderApplicationReview;
import org.example.managementsystem.service.ProviderApplicationReviewService;
import org.example.managementsystem.service.ProviderApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 供应商申请审核表现层
 * @author 王振祎
 * @date 2025-05-21
 */
@RestController
@RequestMapping("/api/provider/application")
public class ProviderApplicationController {

    private final ProviderApplicationService  providerApplicationService;
    private final ProviderApplicationReviewService providerApplicationReviewService;

    @Autowired
    public ProviderApplicationController(ProviderApplicationService providerApplicationService, ProviderApplicationReviewService providerApplicationReviewService) {
        this.providerApplicationService = providerApplicationService;
        this.providerApplicationReviewService = providerApplicationReviewService;
    }

    /**
     * 提交供应商申请
     * @param application
     * @return
     */
    @PostMapping("/submit")
    public ResponseEntity<String> submitApplication(@RequestBody ProviderApplication application) {
        // 保证数据安全，设置申请状态为待审核
        application.setApproved(false);
        application.setStatus(false);
        providerApplicationService.submitApplication(application);
        return ResponseEntity.ok("供应商申请提交成功！");
    }

    /**
     * 更新供应商申请
     * @param application
     * @return
     */
    @PutMapping("/update")
    public ResponseEntity<String> updateApplication(@RequestBody ProviderApplication application) {
        providerApplicationService.updateApplication(application);
        return ResponseEntity.ok("供应商申请更新成功！");
    }

    /**
     * 审核供应商申请并记录审核信息
     * @param review
     * @return
     */
    @PutMapping("/review")
    public ResponseEntity<String> reviewApplication(@RequestBody ProviderApplicationReview review) {
        providerApplicationReviewService.reviewApplication(review);
        return ResponseEntity.ok("供应商申请审核成功！");
    }

    /**
     * 获取所有供应商申请
     * @return
     */
    @GetMapping("/getApplications")
    public ResponseEntity<List<ProviderApplication>> getAllApplications() {
        List<ProviderApplication> applications = providerApplicationService.getAllApplications();
        return ResponseEntity.ok(applications);
    }

    /**
     * 获取所有待审核的供应商申请
     * @return
     */
    @GetMapping("/getUnapprovedApplications")
    public ResponseEntity<List<ProviderApplication>> getAllUnapprovedApplications() {
        List<ProviderApplication> applications = providerApplicationService.getAllUnapprovedApplications();
        return ResponseEntity.ok(applications);
    }

    /**
     * 根据供应商申请ID获取供应商申请
     * @param applicationId
     * @return
     */
    @GetMapping("/getApplicationById/{applicationId}")
    public ResponseEntity<ProviderApplication> getApplicationById(@PathVariable int applicationId) {
        ProviderApplication application = providerApplicationService.getApplicationById(applicationId);
        return ResponseEntity.ok(application);
    }

    /**
     * 根据供应商申请ID获取供应商申请审核信息
     */
    @GetMapping("/getReviewById/{applicationId}")
    public ResponseEntity<ProviderApplicationReview> getReviewById(@PathVariable int applicationId) {
        ProviderApplicationReview review = providerApplicationReviewService.getReviewById(applicationId);
        return ResponseEntity.ok(review);
    }

    /**
     * 根据id删除申请信息
     * @return
     */
    @DeleteMapping("/delete/{applicationId}")
    public ResponseEntity<String> deleteApplication(@PathVariable int applicationId) {
        providerApplicationService.deleteApplication(applicationId);
        return ResponseEntity.ok("删除成功！");
    }

}
